package com.dubbo.common.html;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FilterHTMLUtils {

	/** 获取图片 */
	public static List<String> getImgStr(String htmlStr) {
		List<String> pics = new ArrayList<String>();
		String img = "";
		Pattern p_image;
		Matcher m_image;
		// String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
		String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
		p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
		m_image = p_image.matcher(htmlStr);
		while (m_image.find()) {
			// 得到<img />数据
			img = m_image.group();
			// 匹配<img>中的src数据
			Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
			while (m.find()) {
				pics.add(m.group(1));
			}
		}
		return pics;
	}

	/* 获取纯文本 */
	public static String Html2Text(String inputString) {
		String htmlStr = inputString; // 含html标签的字符串
		String textStr = "";
		Pattern p_script;
		Matcher m_script;
		Pattern p_style;
		Matcher m_style;
		Pattern p_html;
		Matcher m_html;
		try {
			String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>
			String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>
			String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
			p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
			m_script = p_script.matcher(htmlStr);
			htmlStr = m_script.replaceAll(""); // 过滤script标签
			p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
			m_style = p_style.matcher(htmlStr);
			htmlStr = m_style.replaceAll(""); // 过滤style标签
			p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
			m_html = p_html.matcher(htmlStr);
			htmlStr = m_html.replaceAll(""); // 过滤html标签
			textStr = htmlStr;
		} catch (Exception e) {
			System.err.println("Html2Text: " + e.getMessage());
		}
		// 剔除空格行
		textStr = textStr.replaceAll("[ ]+", " ");
		textStr = textStr.replaceAll("(?m)^\\s*$(\\n|\\r\\n)", "");
		return textStr;// 返回文本字符串
	}

	/**
	 * 判断中文和标点符号的逻辑代码
	 * 
	 * 对应规则：中文一对一，英语单词一对一，标点符号一对一，空格回车不算
	 * 
	 * @param value
	 * @return
	 */
	public static int String_length_chinese(String value) {
		int valueLength = 0;
		String chinese = "[\u4e00-\u9fa5]";
		String english = "[a-zA-Z]";
		for (int i = 0; i < value.length(); i++) {
			String temp = value.substring(i, i + 1);
			if (temp.equals(" ")) {
				valueLength += 0;
			} else if (temp.matches(chinese)) {
				valueLength += 1;
			} else if (temp.matches(english)) {
				valueLength += 0;
			} else {
				valueLength += 1;
			}
		}
		return valueLength;
	}

	/**
	 * 判断英文的逻辑代码
	 * 
	 * @param value
	 * @return
	 */
	public static int String_length_english(String value) {
		int valueLength = 0;
		String chinese = "[\u4e00-\u9fa5]";
		String english = "[a-zA-Z]";
		int k = 0;
		for (int i = 0; i < value.length(); i++) {
			String temp = value.substring(i, i + 1);
			if (temp.equals(" ")) {
				if (k == 1) {
					valueLength += 1;
					k = 0;
				}
				valueLength += 0;
			} else if (temp.matches(chinese)) {
				if (k == 1) {
					valueLength += 1;
					k = 0;
				}
				valueLength += 0;
			} else if (temp.matches(english)) {
				if (i == value.length() - 1) {// 循环到最后一位是字母时长度也要加1
					valueLength += 1;
				}
				k = 1;
				valueLength += 0;
			} else {
				if (k == 1) {
					valueLength += 1;
					k = 0;
				}
				valueLength += 0;// 标点符号
			}
		}
		return valueLength;
	}

	// 总长度
	public static int StringLength(String value) {
		int string_length_english = String_length_english(value);
		int string_length_chinese = String_length_chinese(value);
		return string_length_english + string_length_chinese;
	}

}
